package com.nio;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author 咕唧
 * @Date 2022/7/5 21:40
 * @Version 1.0
 * 直接地址内存映射
 */
public class Nio06 {
    public static void main(String[] args) throws Exception {
        //
        RandomAccessFile randomAccessFile = new RandomAccessFile("bb.txt", "rw");
        FileChannel channel = randomAccessFile.getChannel();
        //第一个参数，权限，第二个参数，修改的起始位置，第三个参数，终点位置
        MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 3);
        mappedByteBuffer.put(0, ((byte) 'a'));
        mappedByteBuffer.put(1, ((byte) 'b'));
        randomAccessFile.close();

    }
}
